package com.victor9ac.mashup.es;

import org.elasticsearch.client.Client;
import org.elasticsearch.client.transport.TransportClient;
import org.elasticsearch.common.settings.ImmutableSettings;
import org.elasticsearch.common.transport.InetSocketTransportAddress;

/**
 * Factory used to create clients to connect with elasticsearch
 * 
 * @author victor9ac
 * 
 */
public class ESClientFactory
{

    private static final String CLUSTER_NAME = "cluster.name";
    private static final String CLIENT_TIMEOUT = "client.transport.ping_timeout";

    /**
     * Builds the Search client.
     * 
     * @return the client
     */
    public static final Client build(String clusterName,
                                     String machineUrl,
                                     int machinePort,
                                     String pingTimeout)
    {

        String[] machines = machineUrl.split(",");
        TransportClient client = new TransportClient(ImmutableSettings.settingsBuilder()
                .put(CLUSTER_NAME, clusterName).put(CLIENT_TIMEOUT, pingTimeout).build());

        for (String machine : machines)
        {
            client.addTransportAddress(new InetSocketTransportAddress(machine, machinePort));
        }
        return client;
    }


}